package com.zb.springevent.controller;

import com.zb.springevent.event.UserAddEvent;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author zb
 * @Date 2025/6/16 13:24
 */

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private ApplicationContext applicationContext;


    @RequestMapping("/add")
    public String add(String name) {
        this.notifyEvent(name);
        return "user add success";
    }

    private void notifyEvent(String name) {
        UserAddEvent userAddEvent = new UserAddEvent(this);
        userAddEvent.setTopic("user");
        userAddEvent.setPayload(name);
        applicationContext.publishEvent(userAddEvent);
    }


}
